package com.karry.yl.service.baseinfo.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.yl.dao.baseinfo.BedMapper;
import com.karry.yl.domain.baseinfo.Bed;
import com.karry.yl.domain.baseinfo.BedType;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.domain.elder.ElderlyBebs;
import com.karry.yl.service.baseinfo.BedService;
import com.karry.yl.service.baseinfo.BedTypeService;
import com.karry.yl.service.baseinfo.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class BedServiceImpl extends SuperServiceImpl<BedMapper,Bed> implements BedService {
    @Autowired
    private RoomService roomService;
    @Autowired
    private BedTypeService bedTypeService;
    @Override
    public Page<Bed> selectBedPage(Page<Bed> page, Long orgId,Long buildingId,String floor,Long roomId) {
        return page.setRecords(baseMapper.selectBedList(page, orgId, buildingId, floor, roomId));
    }

    @Override
    public int countByRoomId(Long roomId) {
        return baseMapper.countByRoomId(roomId);
    }

    @Override
    @Transactional
    public void save(Bed bed) {
        baseMapper.insert(bed);
        //给对应房间增加床位数
        Room room = roomService.selectById(bed.getRoomId());
        room.setBedNum(room.getBedNum() + 1);
        //检查某个房间是否满员
        int count = this.countByRoomId(bed.getRoomId());
        if(count >= room.getBedNum()){
            room.setStatus(Room.STATUS_MY);
            roomService.updateById(room);
        }
        roomService.updateById(room);
    }

    @Override
    public void update(Bed bed) {
        baseMapper.updateById(bed);
        Room room = roomService.selectById(bed.getRoomId());
        //检查某个房间是否满员
        int count = this.countByRoomId(bed.getRoomId());
        if(count >= room.getBedNum()){
            room.setStatus(Room.STATUS_MY);
            roomService.updateById(room);
        }else{
            room.setStatus(Room.STATUS_KY);
            roomService.updateById(room);
        }
        roomService.updateById(room);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for(Long id:ids){
            //将房间床位数-1
            Bed bed = this.selectById(id);
            Room room = roomService.selectById(bed.getRoomId());
            room.setBedNum(room.getBedNum() - 1);
            roomService.updateById(room);

            baseMapper.deleteById(id);

        }
    }

    @Override
    @Transactional
    public void saveLsit(String floor,String code[],Long[] typeId,String type[],String roomId,String buildingId,Long orgId) {
    if(type!=null){
        for(int i=0;i<type.length;i++){
            Bed bed = new Bed();
            bed.setCode(code[i]);
            bed.setRoomId(Long.parseLong(roomId));
            bed.setBuildingId(Long.parseLong(buildingId));
            bed.setFloor(floor);
            bed.setOrgId(orgId);
            bed.setType(Integer.parseInt(type[i]));
            bed.setStatus(1);
            baseMapper.insert(bed);
        }
    }else if(typeId!=null){
        for(int i=0;i<typeId.length;i++){
            Bed bed = new Bed();
            bed.setCode(code[i]);
            bed.setRoomId(Long.parseLong(roomId));
            bed.setBuildingId(Long.parseLong(buildingId));
            bed.setFloor(floor);
            bed.setOrgId(orgId);
            bed.setStatus(1);
            BedType bedType=bedTypeService.selectById(typeId[i]);
            bed.setTypeName(bedType.getName());
            bed.setPrice(bedType.getPrice());
            baseMapper.insert(bed);
        }
    }
    }

    @Override
    public List<Bed> selectBedListByBuidFloor(Bed bed) {
        return baseMapper.selectBedListByBuidFloor(bed);
    }

    @Override
    public List<ElderlyBebs> selectBedsByRid(Bed bed) {
        return baseMapper.selectBedsByRid(bed);
    }

    @Override
    public Bed selectBedById(Bed bed) {
        return baseMapper.selectBedById(bed);
    }
}
